#! /bin/bash

# unpacks & installs tbb to '$SYSCPAR_ROOT/tools/tbb30_20100310oss'
# requires:
#	$SYSCPAR_ROOT/downloads/tbb30_20100310oss_src.tgz
#
# type --help for usage
#
# Daniel: some TBB tests failed for me; I think it's ok

usage="$0 --help \t\t - shows this\n$0\t\t\t - installs tbb\n$0 --clean\t\t - removes tbb installed by this script"

case $1 in '-h'|'-help'|'--help')
	echo  -e $usage
	exit;;
esac

[[ $1 == "--clean" ]] && { rm -rf $SYSCPAR_ROOT/tools/tbb30_20100310oss; echo 'cleaned'; exit 0; }

archive='tbb30_20100406oss_src.tgz'
cp $SYSCPAR_ROOT/downloads/$archive $SYSCPAR_ROOT/tools/
cd $SYSCPAR_ROOT/tools/
tar -xzf ./$archive
rm -f ./$archive

mv ./tbb30_20100406oss ./tbb30_20100310oss 

cd ./tbb30_20100310oss
make clean
make

tbb_prefix=$SYSCPAR_ROOT/tools/tbb30_20100310oss
tbb_conf=`make -C $tbb_prefix info | grep 'tbb_build_prefix' | cut -f2 -d=`
cd $tbb_prefix/build/"$tbb_conf"_debug
echo `pwd`
. ../generate_tbbvars.sh
mv ./tbbvars.sh ../

cd ../../examples
some_tests_out=`make | grep 'parallel run total = 1000000\| Done with parallel version.\|Starting game (Parallel evolution for 3 thread(s))' | wc -l`
if [[ $some_tests_out == 3 ]]
then
	echo -e '\E[32;47mTBB has been installed, example suite passed ok (selective check)'; tput sgr0
else
	echo -e '\E[31;47mSomething went wrong during the installation, try installing manualy'; tput sgr0
	rm -rf $SYSCPAR_ROOT/tools/tbb30_20100310oss
fi
